package com.ly.lw.styledo.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.CategoryManListAdapter;
import com.ly.lw.styledo.adapter.CategoryMyGridAdapter;
import com.ly.lw.styledo.bean.CategoryTwo;
import com.ly.lw.styledo.bean.CategoryTwoList;
import com.ly.lw.styledo.config.UrlCategory;
import com.ly.lw.styledo.utils.DensityUtil;
import com.ly.lw.styledo.widget.ElasticScrollView;
import com.ly.lw.styledo.widget.MyScrollGridView;
import com.ly.lw.styledo.widget.MyScrollListView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryManFragment extends Fragment {
    //控件、数据源、适配器
    @ViewInject(R.id.category_my_lv)
    private MyScrollListView listView;
    @ViewInject(R.id.category_my_es)
    private ElasticScrollView es;
    @ViewInject(R.id.category_my_gv)
    private MyScrollGridView gridView;

    private CategoryManListAdapter adapterLv;
    private CategoryMyGridAdapter adapterGv;


    private ArrayList<CategoryTwoList> datasLv;
    private ArrayList<CategoryTwoList> datasGv;

    private int cateNoGrid;
    private int cateNoList;

    public CategoryManFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            cateNoList = getArguments().getInt("cateNoList");
            cateNoGrid = getArguments().getInt("cateNoGrid");
        }
    }

    /**
     * 传参数
     *
     * @param cateNoList
     * @param cateNoGrid
     * @return
     */
    public static CategoryManFragment newInstance(int cateNoList, int cateNoGrid) {
        CategoryManFragment fragment = new CategoryManFragment();
        Bundle args = new Bundle();
        args.putInt("cateNoList", cateNoList);
        args.putInt("cateNoGrid", cateNoGrid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category_dis, container, false);
        ViewUtils.inject(this, view);
        initView();
        initListData();
        initGridData();
        setClickListener();
        return view;
    }


    /**
     * listView监听
     */
    private void setClickListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int y = position * DensityUtil.dip2px(getActivity(), 41);
                es.smoothScrollTo(0, y);
                adapterLv.setSelectItem(position);
                adapterLv.notifyDataSetChanged();

                //gridView的
                cateNoGrid = datasLv.get(position).getCateno();
                datasGv.clear();
                initGridData();
            }
        });
    }

    /**
     * 初始化
     */
    private void initView() {
        datasLv = new ArrayList<>();
        adapterLv = new CategoryManListAdapter(datasLv, getActivity());
        adapterLv.setSelectItem(0);
        listView.setAdapter(adapterLv);

        datasGv = new ArrayList<>();
        adapterGv = new CategoryMyGridAdapter(datasGv, getActivity());
        gridView.setAdapter(adapterGv);
    }

    /**
     * ListView数据
     */
    private void initListData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = getListRequestParms();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlCategory.CATEGORY_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                CategoryTwo categoryTwo = JSONObject.parseObject(result, CategoryTwo.class);
                if (categoryTwo != null) {
                    List<CategoryTwoList> twoList = categoryTwo.getResult().getCtg_list();
                    datasLv.addAll(twoList);
                    adapterLv.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /**
     * GridView数据
     */
    private void initGridData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = getGridRequestParms();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlCategory.CATEGORY_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                CategoryTwo categoryTwo = JSONObject.parseObject(result, CategoryTwo.class);
                if (categoryTwo != null) {
                    List<CategoryTwoList> twoList = categoryTwo.getResult().getCtg_list();
                    datasGv.addAll(twoList);
                    adapterGv.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /**
     * 分类一级页面请求参数
     * 男装  10000050
     * 鞋子  10000082
     * 包包/配饰10000101
     * 首饰/手表10000130
     * 婴幼儿童装10000204
     *
     * @return
     */
    private RequestParams getListRequestParms() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("up_cate_no", cateNoList + "");
        params.addBodyParameter("appVersion", "1.4.6");
        params.addBodyParameter("os", "android");
        params.addBodyParameter("osVersion", "4.1.2");
        return params;
    }

    /**
     * gridView请求路径
     *
     * @return
     */
    private RequestParams getGridRequestParms() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("up_cate_no", cateNoGrid + "");
        params.addBodyParameter("appVersion", "1.4.6");
        params.addBodyParameter("os", "android");
        params.addBodyParameter("osVersion", "4.1.2");
        return params;
    }

}
